package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.Box;
import javax.swing.border.LineBorder;
import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;

import logic.Member;
import logic.Message;
import javax.swing.UIManager;

public class SearchLoan extends JPanel{
	
	public final static int SEARCH_LOAN = 1;
	public final static int SEARCH_FOR_RETURN = 2;
	public final static int SEARCH_FOR_EXTEND = 3;
		
	private Client_GUI parent;
	private JTextField textFieldMemberId;
	private JTextField textFieldBookID;
	private JTextField textFieldLoanDate;
	private JTextField textFieldRetrnDate;
	private JLabel LBLMemberId;
	private JLabel LBLCategory;
	private JLabel LBLBookID;
	private JLabel LBLLoanDate;
	private JLabel LBLRetrnDate;
	private JLabel lblYouMustGive;
	private JLabel lblByLoan;
	
	private SearchLoan self;
	
	private int action;
	
	//TODO set as private
	public JPanel returnPanel;
	
	public SearchLoan(final Client_GUI parent,int action,final JPanel returnPanel) {
		this.self = this;
		this.parent = parent;
		this.returnPanel = returnPanel;
		this.setBackground(UIManager.getColor("inactiveCaption"));
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		this.action = action;
		
		JLabel lblSearchLoan = new JLabel("Search Loan:");
		lblSearchLoan.setBounds(325, 11, 157, 52);
		lblSearchLoan.setFont(new Font("Tahoma", Font.BOLD, 21));
		add(lblSearchLoan);
		
		JLabel lblMemberId = new JLabel("Member Id:");
		lblMemberId.setFont(new Font("Dialog", Font.BOLD, 13));
		lblMemberId.setBounds(213, 149, 92, 14);
		add(lblMemberId);
		
		textFieldMemberId = new JTextField();
		textFieldMemberId.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				lblYouMustGive.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldMemberId.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldMemberId.setColumns(10);
		textFieldMemberId.setBounds(315, 149, 148, 20);
		add(textFieldMemberId);
		
		LBLMemberId = new JLabel("");
		LBLMemberId.setBounds(471, 149, 20, 20);
		add(LBLMemberId);
		
		JLabel lblByMember = new JLabel("By Member:");
		lblByMember.setForeground(Color.RED);
		lblByMember.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblByMember.setBounds(213, 120, 115, 20);
		add(lblByMember);
		
		JLabel lblByBook = new JLabel("By Book:");
		lblByBook.setForeground(Color.RED);
		lblByBook.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblByBook.setBounds(213, 182, 115, 20);
		add(lblByBook);
		
		JLabel lblBookID = new JLabel("Book Id:");
		lblBookID.setFont(new Font("Dialog", Font.BOLD, 13));
		lblBookID.setBounds(213, 221, 92, 14);
		add(lblBookID);
		
		textFieldBookID = new JTextField();
		textFieldBookID.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				lblYouMustGive.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldBookID.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldBookID.setColumns(10);
		textFieldBookID.setBounds(315, 221, 148, 20);
		add(textFieldBookID);
		
		JLabel lblLoanDate = new JLabel("Loan Date:");
		lblLoanDate.setFont(new Font("Dialog", Font.BOLD, 13));
		lblLoanDate.setBounds(213, 299, 92, 14);
		add(lblLoanDate);
		
		textFieldLoanDate = new JTextField();
		textFieldLoanDate.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				lblYouMustGive.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldLoanDate.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldLoanDate.setColumns(10);
		textFieldLoanDate.setBounds(315, 299, 148, 20);
		add(textFieldLoanDate);
		
		textFieldRetrnDate = new JTextField();
		textFieldRetrnDate.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				lblYouMustGive.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldRetrnDate.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldRetrnDate.setColumns(10);
		textFieldRetrnDate.setBounds(315, 324, 148, 20);
		add(textFieldRetrnDate);
		
		JLabel lblRetrnDate = new JLabel("Retrn Date:");
		lblRetrnDate.setFont(new Font("Dialog", Font.BOLD, 13));
		lblRetrnDate.setBounds(213, 324, 92, 14);
		add(lblRetrnDate);
		
		LBLBookID = new JLabel("");
		LBLBookID.setBounds(471, 221, 20, 20);
		add(LBLBookID);
		
		LBLLoanDate = new JLabel("");
		LBLLoanDate.setBounds(471, 299, 20, 20);
		add(LBLLoanDate);
		
		LBLRetrnDate = new JLabel("");
		LBLRetrnDate.setBounds(471, 324, 20, 20);
		add(LBLRetrnDate);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ValidateData(true);			
			}
		});
		btnSearch.setBounds(146, 425, 121, 37);
		add(btnSearch);
		
		JButton button = new JButton("Cancel");
		button.setFont(new Font("Tahoma", Font.BOLD, 13));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				parent.RepaintMenu(parent.client.Privelege);				
			}
		});
		button.setBounds(560, 425, 115, 37);
		add(button);
		
		lblYouMustGive = new JLabel("You must give at least one  input!!!!");
		lblYouMustGive.setForeground(Color.RED);
		lblYouMustGive.setFont(new Font("Dialog", Font.BOLD, 13));
		lblYouMustGive.setBounds(252, 372, 320, 32);
		add(lblYouMustGive);
		
		lblByLoan = new JLabel("By Loan:");
		lblByLoan.setForeground(Color.RED);
		lblByLoan.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblByLoan.setBounds(213, 264, 115, 20);
		add(lblByLoan);
		
		
		
		JButton btnNewButton = new JButton("Search Book");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SearchBook topaint =new SearchBook(parent,SearchBook.SEARCH_FOR_SEARCH_LOAN,self);
				parent.currentMain = topaint;
				parent.RepaintMain(topaint);
			}
		});
		btnNewButton.setBounds(517, 219, 115, 24);
		add(btnNewButton);
		
		JButton btnSearchMember = new JButton("Search Member");
		btnSearchMember.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SearchMember topaint =new SearchMember(parent,SearchMember.SEARCH_FOR_SEARCH_LOAN,self);
				parent.currentMain = topaint;
				parent.RepaintMain(topaint);
			}
		});
		btnSearchMember.setBounds(517, 145, 115, 24);
		add(btnSearchMember);
		lblYouMustGive.setVisible(false);
		
		
		
		if(parent.client.Privelege == Member.MEMBER_PRIVILIGE){
			this.setEnabled(false);
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();		
			
			index.add("ACTION");
			data.add(action);
			
			index.add("UID");
			data.add(parent.client.GetUID());
		
			
			int actionOut = Message.SEARCH_LOAN_REQUEST;
	
			Message toSend = new Message(index, data, actionOut);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
							
			}else{//message send failed
			}
		}
	}
	
	
	
		public void SetUID(String uid){
			textFieldMemberId.setText(uid);
		}
	
		public void SetBID(String bid){
			textFieldBookID.setText(bid);
		}
		
		
		
		
		
		
		public void SetLoad(String uid){
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();		
			
			index.add("ACTION");
			data.add(action);
			
			index.add("UID");
			data.add(uid);
			
			int actionOut = Message.SEARCH_LOAN_REQUEST;
	
			Message toSend = new Message(index, data, actionOut);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
							
			}else{//message send failed
				//TODO verify extream situation
			}
		}
		
		
		
		
	
		private void ValidateData(boolean fromOK){
		
		ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
		ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
		ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		boolean valid = true;
		
		String temp; // the string to be tested on each test
		
		
		//validate Loan date
		temp = textFieldLoanDate.getText();
		if(temp.length()== 10)
		try
		{
			if ((temp.charAt(4) == '-' && temp.charAt(7) == '-') || (temp.charAt(4) == '/' && temp.charAt(7) == '/') || (temp.charAt(4) == '.' && temp.charAt(7) == '.')) 
				LBLLoanDate.setIcon(OK);
			else {
				LBLLoanDate.setIcon(BAD);
				valid = false;
			}
			
			int year = Integer.parseInt(temp.substring(0, 4));
			int month = Integer.parseInt(temp.substring(5, 7));
			int day = Integer.parseInt(temp.substring(8, temp.length()));
	
			Calendar validator = Calendar.getInstance();
			validator.set(year, month-1, day);
			validator.setLenient(false);
			validator.getTime();
			
		}catch(NumberFormatException e){
			LBLLoanDate.setIcon(BAD);
			valid = false;
		}catch(Exception e){
			LBLLoanDate.setIcon(BAD);
			valid = false;
		}
		else if(!temp.equals("")) {
			LBLLoanDate.setIcon(BAD);
			valid = false;
		}
		else LBLLoanDate.setIcon(null);
		
		
		//validate Return  date
		temp =textFieldRetrnDate.getText();
		if(temp.length() == 10)
			try
			{
				if ((temp.charAt(4) == '-' && temp.charAt(7) == '-') || (temp.charAt(4) == '/' && temp.charAt(7) == '/') || (temp.charAt(4) == '.' && temp.charAt(7) == '.')) 
					LBLRetrnDate.setIcon(OK);
				else {
					LBLRetrnDate.setIcon(BAD);
					valid = false;
				}
				
				int year = Integer.parseInt(temp.substring(0, 4));
				int month = Integer.parseInt(temp.substring(5, 7));
				int day = Integer.parseInt(temp.substring(8, temp.length()));
	
				Calendar validator = Calendar.getInstance();
				validator.set(year, month-1, day);
				validator.setLenient(false);
				validator.getTime();
				
			}catch(NumberFormatException e){
				LBLRetrnDate.setIcon(BAD);
				valid = false;
			}catch(Exception e){
				LBLRetrnDate.setIcon(BAD);
				valid = false;
			}
			else if(!temp.equals("")) {
				LBLRetrnDate.setIcon(BAD);
				valid = false;
			}
			else LBLRetrnDate.setIcon(null);
		
	
		//Validate UID
		temp = textFieldMemberId.getText();
		if (!temp.equals(""))
		try{
			char c = temp.charAt(0);
			if (c == '+' || c == '-') {
				LBLMemberId.setIcon(BAD);
				valid = false;
			}
			else LBLMemberId.setIcon(OK);
	
			
			Integer.parseInt(temp);	
		}catch(NumberFormatException e){
			LBLMemberId.setIcon(BAD);
			valid = false;
		}
		else LBLMemberId.setIcon(null);
		
		//Validate BOOK ID
		temp = textFieldBookID.getText();
		if (!temp.equals(""))
		try{
			char c = temp.charAt(0);
			if (c == '+' || c == '-') {
				LBLBookID.setIcon(BAD);
				valid = false;
			}
			else LBLBookID.setIcon(OK);
	
			
			Integer.parseInt(temp);	
		}catch(NumberFormatException e){
			LBLBookID.setIcon(BAD);
			valid = false;
		}
		else LBLBookID.setIcon(null);
		
		
		
		if (fromOK){
			
			
			if (textFieldBookID.getText().equals("")&& textFieldMemberId.getText().equals("")&&
					textFieldLoanDate.getText().equals("") && textFieldRetrnDate.getText().equals("")){
				lblYouMustGive.setVisible(true);
				valid = false;
			}
		}
		
		
		if (valid && fromOK){
			
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();		
			
			index.add("ACTION");
			data.add(action);
			
			if(!textFieldMemberId.getText().equals("")){
				index.add("UID");
				data.add(textFieldMemberId.getText());
			}
			if(!textFieldBookID.getText().equals("")){
				index.add("BID");
				data.add(textFieldBookID.getText());
			}
			if(!textFieldRetrnDate.getText().equals("")) {
				index.add("RETURN_DATE");
				data.add(textFieldRetrnDate.getText());
			}
			if(!textFieldLoanDate.getText().equals("")){
				index.add("LOAN_DATE");
				data.add(textFieldLoanDate.getText());
			}
	
			
			
			
			int action = Message.SEARCH_LOAN_REQUEST;
	
			Message toSend = new Message(index, data, action);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
							
			}else{//message send failed
				//TODO verify extream situation
			}
			
		
	}
	
	
	}
}
